package org.tis.tools.core.web.vo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.validation.constraints.NotNull;

/**
 * describe: 分页请求对象
 *
 * @author zhaoch
 * @date 2018/4/3
 **/
public class SmartPage<T> {

    @NotNull(message = "分页参数不能为空")
    private PageVO page;

    private T condition;

    public PageVO getPage() {
        return page;
    }

    public void setPage(PageVO page) {
        this.page = page;
    }

    public T getCondition() {
        return condition;
    }

    public void setCondition(T condition) {
        this.condition = condition;
    }

    public Page<T> getPageObj() {
        Page<T> p = new Page<>(page.getCurrent(), page.getSize());
        if (page.getOrderByField() != null) {
            p.setOrderByField(page.getOrderByField());
            p.setAsc(page.getAsc());
        }
        return p;
    }

    /**
     * JSON序列化忽略，解决swagger警告：OptimizedModelPropertiesProvider : Unable to get unique property
     * @return
     */
    @JsonIgnore
    public EntityWrapper<T> getConditionWrapper() {
        return new EntityWrapper<>(condition);
    }
}
